package com.bfly.mall.shop.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bfly.common.ResponseUtil;
import com.bfly.common.json.JsonUtil;
import com.bfly.common.util.TenCentMapUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.cache.SysConfigCacheUtil;
import com.bfly.mall.shop.entity.Shop;
import com.bfly.mall.shop.dto.ShopFormDTO;
import com.bfly.mall.shop.service.IShopService;
import com.bfly.mall.system.dto.WXMiniProConfigDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/11/27 19:38
 */
@RestController
@RequestMapping(value = "/api/shop")
public class ShopApiController extends BaseController {

    @Autowired
    private IShopService shopService;

    /**
     * 门店信息
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/27 19:39
     */
    @GetMapping(value = "/info")
    public void getShop(HttpServletResponse response) {
        Shop shop = shopService.getShop();
        ShopFormDTO dto = new ShopFormDTO();
        if (shop != null) {
            BeanUtils.copyProperties(shop, dto);
        }
        JSONObject json= JsonUtil.toJsonFilter(dto);
        WXMiniProConfigDTO config = SysConfigCacheUtil.getWxMiniProConfig();
        if(config!=null) {
            String appName = config.getName();
            json.put("appName", appName);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(json));
    }

    /**
     * 测离门店的距离
     * @author 长沙商脉科技有限公司
     * @date 2020/12/1 17:04
     */
    @GetMapping(value = "/distance")
    public void getDistance(HttpServletResponse response, @RequestParam("from")String from) throws Exception{
        String points[]=from.split(","); //坐标系
        JSONArray array=null;
        if(points.length==2){
            Shop shop=shopService.getShop();
            if(StringUtils.isNotBlank(shop.getLatitude()) && StringUtils.isNotBlank(shop.getLongitude())) {
                array = TenCentMapUtil.distance(from, shop.getLatitude() + "," + shop.getLongitude());
            }
        }
        ResponseUtil.writeJson(response,ResponseData.getSuccess(array));
    }
}